package com.dhcc.bpm.alarm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dhcc.bpm.alarm.entity.AlarmNotice;
import com.dhcc.bpm.alarm.entity.AlarmStrategy;
import com.dhcc.bpm.alarm.service.IAlarmNoticeInfoService;
import com.dhcc.bpm.alarm.service.IAlarmRecordService;
import com.dhcc.bpm.alarm.service.IAlarmStrategyInfoService;
import com.dhcc.bpm.alarm.service.WarningExecutionService;
import com.dhcc.bpm.alarm.service.impl.TaskAsyncService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
@RestController
@AllArgsConstructor
@NoArgsConstructor
@RequestMapping("api/task")
@Api(value = "", tags = "触发通知执行接口")
@Slf4j
public class WarningExecutionController {
    @Autowired
    private WarningExecutionService warningExecutionService;
    @Autowired
    private IAlarmNoticeInfoService alarmNoticeInfoService;
    @Autowired
    private IAlarmStrategyInfoService alarmStrategyInfoService;
    @Autowired
    private IAlarmRecordService alarmRecordService;
    @Autowired
	TaskAsyncService taskAsyncService;

    @PostMapping("/warningExecution")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "执行定时任务告警", notes = "传入批次号和流水号")
    public JSONObject warningExecution(@Valid @RequestBody String json) {
        //批次号
        JSONObject data = JSON.parseObject(json);
        String pkTaskNo = data.getString("pkTaskNo");
        //流水号
        String batchTaskNo = data.getString("traceNo");
        MDC.put("traceNo", batchTaskNo);
        log.info("执行定时任务告警  pkTaskNo:{}, traceNo:{}", pkTaskNo, batchTaskNo);
        
        JSONObject result = new JSONObject();
        result.put("traceNo", batchTaskNo);
        if(StringUtils.isBlank(pkTaskNo)||StringUtils.isBlank(batchTaskNo)){
            log.error("批次号或者流水号不能为空");
            result.put("code","1111");
            result.put("msg","批次号或者流水号不能为空");
            taskAsyncService.resultNotice(data, "03", "批次号或者流水号不能为空"); // 异步通知
            return result;
        }
        
        try {
            warningExecutionService.exec(pkTaskNo);
            result.put("code","0000");
            result.put("msg","执行成功");
            taskAsyncService.resultNotice(data, "00", "执行成功"); // 异步通知
        } catch (Exception e) {
        	e.printStackTrace();
            result.put("code","0101");
            result.put("msg",e.getMessage());
            taskAsyncService.resultNotice(data, "03", e.getMessage()); // 异步通知
        }
        MDC.remove("traceNo");
        return result;
    }
    @PostMapping("/eventNotice")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "进行事件触发的告警通知", notes = "系统编号、告警名称、告警编号、预警日期、预警流水、告警消息通知方式、消息内容类型、告警消息内容、消息要素")
    public JSONObject eventNotice(@Valid @RequestBody String json) {
        JSONObject data = JSON.parseObject(json);
        //系统编号
        String systemNo = data.getString("systemNo");
        //告警名称
        String noticeName = data.getString("noticeName");
        //告警编号
        String noticeCode = data.getString("noticeCode");
        //预警日期
        String noticeDate = data.getString("noticeDate");
        //预警流水
        String noticeTranno = data.getString("noticeTranno");
        //告警消息通知方式
        String noticeMethod = data.getString("noticeMethod");
        //消息内容类型
        String noticeContent = data.getString("noticeContent");
        //告警消息内容
        String noticeMessage = data.getString("noticeMessage");
        //消息要素
        String partMessage = data.getString("partMessage");
        JSONObject result = new JSONObject();

        if(StringUtils.isBlank(systemNo)||StringUtils.isBlank(noticeCode)||StringUtils.isBlank(noticeTranno)
               ||StringUtils.isBlank(noticeMessage)){
            log.error("参数校验不通过systemNo:{},noticeCode:{},noticeTranno:{},noticeMessage:{}",systemNo,noticeCode,noticeTranno,noticeMessage);
            result.put("retCode",01);
            result.put("retMsg","批次号或者流水号不能为空");
            return result;
        }
        MDC.put("traceNo",noticeTranno);
        try {
            AlarmNotice alarmNotice = alarmNoticeInfoService.getOne(new QueryWrapper<AlarmNotice>().eq("NOTICE_CODE",noticeCode));
            //如果不传通知方式，就按默认的来
            if(!StringUtils.isBlank(noticeMethod)){
                alarmNotice.setNoticeMethod(noticeMethod);
            }
            AlarmStrategy alarmStrategy = alarmStrategyInfoService.getOne(new QueryWrapper<AlarmStrategy>().eq("STRATEGY_CODE",alarmNotice.getStrategyCode()));
            alarmRecordService.insertAlarmRecord(null,alarmStrategy,alarmNotice,noticeMessage,noticeContent,null);
            result.put("retCode",00);
            result.put("retMsg","执行成功");
        } catch (Exception e) {
            log.error("告警通知发送失败",e);
            result.put("retCode",01);
            result.put("retMsg",e.getMessage());
        }
        MDC.remove("traceNo");
        return result;
    }
}
